package com.webserver.controller;

import com.webserver.annotations.Controller;
import com.webserver.annotations.RequestMapping;
import com.webserver.http.HttpServletRequest;
import com.webserver.http.HttpServletResponse;
import com.webserver.util.DBUtil;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@Controller
public class ArticleController {

    @RequestMapping("/writeArticle")
    public void write(HttpServletRequest request, HttpServletResponse response){
        System.out.println("开始处理发表文章功能！！！！");
        //1获取表单信息
        String title = request.getParameter("title");
        String author = request.getParameter("author");
        String content = request.getParameter("content");
        /*
            有一个验证:如果作者无效，要提示一个页面，该作者不存在!

            1:先根据该文章的作者名去userinfo表中找到该用户，并得到该用户的id(主键值)
            2:将文章信息插入到article表中，u_id字段插入该用户的主键值
         */
        try(
                Connection connection = DBUtil.getConnection();
        ){
            String sql = "SELECT id FROM userinfo WHERE username=?";
            PreparedStatement ps = connection.prepareStatement(sql);
            ps.setString(1,author);
            ResultSet rs = ps.executeQuery();
            if(rs.next()){
                //获取该用户id
//                int id = rs.getInt(1);
                int id = rs.getInt("id");
                sql = "INSERT INTO article (title,content,u_id) VALUES (?,?,?)";
                ps = connection.prepareStatement(sql);
                ps.setString(1,title);
                ps.setString(2,content);
                ps.setInt(3,id);
                int num = ps.executeUpdate();
                if(num>0){
                    response.sendRedirect("/write_article_success.html");
                }else{
                    response.sendRedirect("/write_article_fail.html");
                }

            }else{//没有此作者
                response.sendRedirect("/have_not_user.html");
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }


    }
}
